{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "de765f16-341b-4dc3-b59d-b16437c7e050",
   "metadata": {},
   "source": [
    "\n",
    "# Ollama Embedding\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e2fb929-255e-476a-8839-baddcbfadd26",
   "metadata": {},
   "source": [
    "## nomic-embed-text\n",
    "\n",
    "https://ollama.com/library/nomic-embed-text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e25cdbdc-daee-4a9b-b363-9080dc216927",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: langchain\n",
      "Version: 0.3.7\n",
      "Summary: Building applications with LLMs through composability\n",
      "Home-page: https://github.com/langchain-ai/langchain\n",
      "Author: \n",
      "Author-email: \n",
      "License: MIT\n",
      "Location: /opt/conda/lib/python3.11/site-packages\n",
      "Requires: aiohttp, langchain-core, langchain-text-splitters, langsmith, numpy, pydantic, PyYAML, requests, SQLAlchemy, tenacity\n",
      "Required-by: langchain-community\n",
      "---\n",
      "Name: langchain-ollama\n",
      "Version: 0.2.0\n",
      "Summary: An integration package connecting Ollama and LangChain\n",
      "Home-page: https://github.com/langchain-ai/langchain\n",
      "Author: \n",
      "Author-email: \n",
      "License: MIT\n",
      "Location: /opt/conda/lib/python3.11/site-packages\n",
      "Requires: langchain-core, ollama\n",
      "Required-by: \n"
     ]
    }
   ],
   "source": [
    "!pip show langchain langchain_ollama"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e07b2c7d-62d7-457c-bbcb-298b29aa522c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_ollama import OllamaEmbeddings\n",
    "\n",
    "ollama_url = \"http://ollama:11434\"\n",
    "\n",
    "model_name = \"nomic-embed-text:v1.5\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "53c0c24f-5b42-45e5-839f-f318d6a52eea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 12.6 ms, sys: 3.82 ms, total: 16.4 ms\n",
      "Wall time: 15.5 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "ollama_emb = OllamaEmbeddings(\n",
    "    base_url = ollama_url,\n",
    "    model=model_name\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ceb8891-f838-4d9d-8996-9f5c16b914d7",
   "metadata": {},
   "source": [
    "### embed_query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c99e1b52-f356-4ed9-8a16-a26c970e920e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 2.85 ms, sys: 984 µs, total: 3.83 ms\n",
      "Wall time: 106 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# embed the text and return a vector\n",
    "query_vector = ollama_emb.embed_query(\"The sky is blue because of Rayleigh scattering\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "348e80b7-2e83-4635-8c2b-208cbc74fbdf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vector size: 768\n"
     ]
    }
   ],
   "source": [
    "# print the vector size\n",
    "print(f'Vector size: {len(query_vector)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b0a7dac-d822-4fa7-b46e-99865fe0ffce",
   "metadata": {},
   "source": [
    "### embed_documents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "48eb95a9-1d3f-47bc-9269-e500736427eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 2.21 ms, sys: 1.04 ms, total: 3.25 ms\n",
      "Wall time: 305 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "documents_vector = ollama_emb.embed_documents(\n",
    "    [\n",
    "        \"Alpha is the first letter of Greek alphabet\",\n",
    "        \"Beta is the second letter of Greek alphabet\",\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1f6169af-9b0f-4640-86fc-af900c9313d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vector size: 768\n"
     ]
    }
   ],
   "source": [
    "# print the vector size\n",
    "print(f'Vector size: {len(documents_vector[0])}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea8ddebb-55df-4dca-953e-cd6998609b9e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
